package weather.app.pack;


import android.os.Bundle;
import android.app.ListActivity;
import android.content.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity
{
	//ActivityTwo
    protected static final int REQUEST_CODE = 0;
    //ActivityThree
	protected static final int REQUEST_CODEThird = 10;
	
    CityAdapter adapter;
 
    @Override
    public void onCreate (Bundle savedInstanceBundle)
    {
    	super.onCreate (savedInstanceBundle);
        adapter = new CityAdapter (this);
        setContentView (R.layout.main);
        setListAdapter (adapter);
        
        /**
         * OnItemLongClickListener - REMOVE CITY FROM LIST
         */
        getListView().setOnItemLongClickListener(new OnItemLongClickListener() 
		{
        	public boolean onItemLongClick(AdapterView<?> listAdapter, View view,int position, long id) 
			{
        		//get item to remove from list
               String c = (String)adapter.getItem (position);
               
               adapter.removeCity(position);
               
               Toast.makeText(MainActivity.this, c+ " removed from the list", Toast.LENGTH_LONG).show();
               
               return true;
			}
		});// end OnItemLongClickListener
        
        //Add New City button
        final Button button = (Button) findViewById(R.id.button);
        /**
         * OnClickListener
         */
        button.setOnClickListener(new OnClickListener() 
        {
            public void onClick(View v) 
            {
                Intent i = new Intent();
                
                i.setClass(MainActivity.this, ActivityTwo.class);
        		
        		startActivityForResult(i, REQUEST_CODE);
            }
        });// end OnClickListener
        
    }// end onCreate
 
    
    /**
     * onActivityResult
     */
    
    @Override
    public void onActivityResult (int requestCode, int responseCode, Intent data)
    {
    	// ActivityTwo - requestCode = 0
    	
    	if (requestCode == 0)
    	{
    		if (responseCode==1)
    		{
    			 if (data.hasExtra("name")) 
    			 {
    				 //addCity
    				adapter.addCity(data.getExtras().getString("name"));
    			 }
    			
    		}
    	
    		else if (responseCode==0){
    		}
    	}// end if 
    	
    }// end onActivityResult
    
    /**
     * onListItemClick - SHOW FORECAST FOR SELECTED CITY
     */
    @Override
    public void onListItemClick (ListView list, View v, int position, long id)
    {
        
        String c = (String)adapter.getItem (position);
        
        Toast.makeText(this, c + " selected", Toast.LENGTH_LONG).show();
		
        Intent in = new Intent();
        
        in.setClass(MainActivity.this, ActivityThree.class);
        
		in.putExtra("city", c);
		setResult(1,in);
		
        startActivityForResult(in, REQUEST_CODEThird);
        
    }// end onListItemClick
    
}// end MainActivity